package com.crm.api.service.impl;

import com.crm.api.entity.BusinessType;
import com.crm.api.mapper.BusinessTypeMapper;
import com.crm.api.service.BusinessTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务类型服务实现类
 */
@Slf4j
@Service
public class BusinessTypeServiceImpl implements BusinessTypeService {

    @Autowired
    private BusinessTypeMapper businessTypeMapper;

    @Override
    public Map<String, Object> getBusinessTypes(Integer page, Integer size, String typeName, Boolean isActive) {
        // 计算偏移量
        Integer offset = (page - 1) * size;
        
        // 构建查询参数
        Map<String, Object> params = new HashMap<>();
        params.put("offset", offset);
        params.put("pageSize", size);
        params.put("limit", size);  // 为了兼容性保留
        params.put("typeName", typeName);
        params.put("isActive", isActive);
        
        // 查询数据
        List<BusinessType> businessTypes = businessTypeMapper.findBusinessTypes(params);
        Integer total = businessTypeMapper.countBusinessTypes(params);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("data", businessTypes);
        result.put("total", total);
        result.put("page", page);
        result.put("size", size);
        result.put("totalPages", (total + size - 1) / size);
        
        return result;
    }

    @Override
    public List<BusinessType> getActiveBusinessTypes() {
        return businessTypeMapper.findActiveBusinessTypes();
    }

    @Override
    public BusinessType getBusinessTypeById(Integer id) {
        return businessTypeMapper.findById(id);
    }

    @Override
    public BusinessType createBusinessType(BusinessType businessType) {
        // 设置默认值
        if (businessType.getIsActive() == null) {
            businessType.setIsActive(true); // 默认启用
        }
        
        // 设置创建时间
        businessType.setCreatedAt(new Date());
        businessType.setUpdatedAt(new Date());
        
        // 插入数据
        businessTypeMapper.insert(businessType);
        
        return businessType;
    }

    @Override
    public BusinessType updateBusinessType(BusinessType businessType) {
        // 检查业务类型是否存在
        BusinessType existingBusinessType = businessTypeMapper.findById(businessType.getId());
        if (existingBusinessType == null) {
            return null;
        }
        
        // 设置更新时间
        businessType.setUpdatedAt(new Date());
        
        // 更新数据
        businessTypeMapper.update(businessType);
        
        return businessTypeMapper.findById(businessType.getId());
    }

    @Override
    public boolean deleteBusinessType(Integer id) {
        // 检查业务类型是否存在
        BusinessType businessType = businessTypeMapper.findById(id);
        if (businessType == null) {
            return false;
        }
        
        // 删除业务类型
        businessTypeMapper.deleteById(id);
        return true;
    }

    @Override
    public boolean updateBusinessTypeStatus(Integer id, Boolean isActive) {
        // 检查业务类型是否存在
        BusinessType businessType = businessTypeMapper.findById(id);
        if (businessType == null) {
            return false;
        }
        
        // 更新状态
        businessTypeMapper.updateStatus(id, isActive, new Date());
        return true;
    }

    @Override
    public boolean existsByTypeName(String typeName) {
        return businessTypeMapper.existsByTypeName(typeName) > 0;
    }

    @Override
    public boolean existsByTypeNameAndNotId(String typeName, Integer id) {
        return businessTypeMapper.existsByTypeNameAndNotId(typeName, id) > 0;
    }

    @Override
    public boolean hasRelatedBusinesses(Integer id) {
        return businessTypeMapper.countRelatedBusinesses(id) > 0;
    }
}